﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

using Omega.Game.Framework;



namespace Omega.Game.Framework
{
    [Serializable]
    public class MapData
    {



  
        public string Notes { get; set; }

        public string PanormaName { get; set; }
        public string FogName { get; set; }
        //public Plane Panorama { get; set; }
        //public Plane Fog { get; set; }
        public int Panorama_X { get; set; }
        public float Panorama_Y { get; set; }
        public float Fog_X { get; set; }
        public float Fog_Y { get; set; }

        public Color LightColor = System.Drawing.Color.White; 
        public int IntencidaeLuz = 200;
        /// <summary>
        /// Retorna a Largura do mapa em tilesk
        /// </summary>
        public int Width
        {
            get
            {
                if ( Tilemap == null ) return 0;
                return this.Tilemap.Layers[ 0 ].Width;
            }
        }

        /// <summary>
        /// Retorna a altura do mapa em tiles
        /// </summary>
        public int Height
        {
            get
            {
                if ( Tilemap == null ) return 0;
                return this.Tilemap.Layers[ 0 ].Height;
            }
        }

        public int ID { get; set; }

        public String Name { get; set; }

        public Tilemap Tilemap { get; set; }

        public virtual void Reload( )
        {
            Dispose( );
            Load( );
        }


        public MapData( int id , String name , int width , int height )
        {
            this.ID = id;
            this.Name = name;

            this.Tilemap = new Tilemap( width , height );
            PanormaName = "";
            FogName = "";
            Panorama_Y = 0;
            Panorama_X = 0;
            Fog_Y = 0;
            Fog_X = 0;
            //if ( _lightsObjects == null ) _lightsObjects = new Light[ Height , Width ];
            //if ( _mapObjects == null ) _mapObjects = new GameObject[ Height , Width ];
            //LightColor = new Color( 255 , 255 , 255 );

        }

        public void ResizeMap( int width , int height )
        {
            this.Dispose( );
            this.ResizeObjectsLayers( width , height );
            this.Tilemap.Resize( width , height );
            this.Load( );
        }

        private void ResizeObjectsLayers( int width , int height )
        {
            //GameObject[ , ] tmpob = new GameObject[ height , width ];
            //Light[ , ] tml = new Light[ height , width ];


            for ( int x = 0 ; x < width ; x++ )
            {
                for ( int y = 0 ; y < height ; y++ )
                {
                    //if ( x < _lightsObjects.GetLength( 1 ) && y < _lightsObjects.GetLength( 0 ) )
                    //{
                    //    tml[ y , x ] = _lightsObjects[ y , x ];
                    //    tmpob[ y , x ] = _mapObjects[ y , x ];
                    //}
                }

            }
            //_lightsObjects = new Light[ height , width ];
            //_mapObjects = new GameObject[ height , width ];

            for ( int x = 0 ; x < width ; x++ )
            {
                for ( int y = 0 ; y < height ; y++ )
                {
                    //if ( x < _lightsObjects.GetLength( 1 ) && y < _lightsObjects.GetLength( 0 ) )
                    //{
                    //    _lightsObjects[ y , x ] = tml[ y , x ];
                    //    _mapObjects[ y , x ] = tmpob[ y , x ];
                    //}
                }

            }

        }

        public void Load( )
        {
            //Engines.Lights.SetMaskColor( LightColor == null ? new Color()  : LightColor
            //    , IntencidaeLuz );

            //if ( _lightsObjects == null ) _lightsObjects = new Light[ Height , Width ];
            //if ( _mapObjects == null ) _mapObjects = new GameObject[ Height , Width ];

            Tilemap.Load( );

            for ( int x = 0 ; x < Width ; x++ )
            {
                for ( int y = 0 ; y < Height ; y++ )
                {
                    //if ( _mapObjects[ y , x ] != null ) _mapObjects[ y , x ].Load( );
                    //if ( _lightsObjects[ y , x ] != null ) _lightsObjects[ y , x ].Load( );
                }

            }
        }
        public void Dispose( )
        {
            Tilemap.Dispose( );
            for ( int x = 0 ; x < Width ; x++ )
            {
                for ( int y = 0 ; y < Height ; y++ )
                {
                    //if ( _mapObjects[ y , x ] != null ) _mapObjects[ y , x ].Dispose( );
                    //if ( _lightsObjects[ y , x ] != null ) _lightsObjects[ y , x ].Dispose( );
                }

            }

        }

        public Tile GetTile( int x , int y , int Deph )
        {
            return this.Tilemap.Layers[ Deph ].GetTileAt( x , y );
        }


        public void Update( )
        {
            if ( !String.IsNullOrEmpty( this.PanormaName ) )
            {
            //    if ( Panorama != null )
            //    {
            //        if ( Panorama.BitmpName != this.PanormaName )
            //        {
            //            Panorama.Dispose( );
            //            Panorama = null;
            //            Panorama = new Plane(  GameEnvironment.PanoramaPath + PanormaName );
            //        }
            //    }
            //    else
            //    {
            //        Panorama = new Plane(GameEnvironment.PanoramaPath + PanormaName );
            //    }

            //}
            //else
            //{
            //    if ( Panorama != null )
            //    {
            //        Panorama.Dispose( );
            //        Panorama = null;
            //    }

            //}
            //if ( !String.IsNullOrEmpty( this.FogName ) )
            //{
            //    if ( Fog != null )
            //    {
            //        if ( Fog.BitmpName != FogName )
            //        {
            //            Fog.Dispose( );
            //            Fog = null;
            //            Fog = new Plane(GameEnvironment.FogsPath +  FogName );
            //            Fog.Z = 0.9f;
            //            Fog.Opacity = 50;
            //        }
            //    }
            //    else
            //    {
            //        Fog = new Plane(GameEnvironment.FogsPath + FogName );
            //        Fog.Z = 0.9f;
            //        Fog.Opacity = 50;
            //    }

            //}
            //else
            //{
            //    if ( Fog != null )
            //    {
            //        Fog.Dispose( );
            //        Fog = null;
            //    }
            //}
        }

        /// <summary>
        /// Returns a Rectangle in pixels of map visible area!
        /// </summary>
        /// <returns>Rectangle</returns>
        //public Rectangle VisibleClipBounds( )
        //{
        //    //int x = Graphics.Runtime.CameraOffSet_X * 32;
        //    //int y = Graphics.Runtime.CameraOffSet_Y * 32;
        //    //int width = Width < ( Graphics.Runtime.Width / 32 ) ? Width * 32 : Graphics.Runtime.Width / 32 * 32;
        //    //int height = Height < ( Graphics.Runtime.Height / 32 ) ? Height * 32 : Graphics.Runtime.Height / 32 * 32;

        //    if ( width <= 0 ) width = Width * 32;
        //    if ( height <= 0 ) height = Height * 32;

        //    return new Rectangle( x , y , width , height );
        //}

        /// <summary>
        /// Returns a Rectangle in tiles of map visible area!
        /// </summary>
        /// <returns></returns>
        //public Rectangle VisibleRectangleTiles( )
        //{
        //    int x = Graphics.Runtime.CameraOffSet_X;
        //    int y = Graphics.Runtime.CameraOffSet_Y;
        //    int width = Width < ( Graphics.Runtime.Width / 32 ) ? Width : Graphics.Runtime.Width / 32;
        //    int height = Height < ( Graphics.Runtime.Height / 32 ) ? Height : Graphics.Runtime.Height / 32;

        //    if ( width <= 0 ) width = Width;
        //    if ( height <= 0 ) height = Height;

        //    return new Rectangle( x , y , width , height );
        //}

        /// <summary>
        /// Returns the visible Width and Height of the map in tiles
        /// </summary>
        /// <returns>Size</returns>
        //public Size VisibleSize
        //{
        //    get
        //    {
        //        int COX = Omega.Game.Framework.Graphics.Runtime.CameraOffSet_X;
        //        int COY = Omega.Game.Framework.Graphics.Runtime.CameraOffSet_Y;

        //        int nwd = this.Width - COX;
        //        int nhg = this.Height - COY;

        //        int newx = ( nwd > 0 ? nwd : Omega.Game.Framework.Graphics.Runtime.Width / 32 );
        //        int newY = ( nhg > 0 ? nhg : Omega.Game.Framework.Graphics.Runtime.Height / 32 );

        //        int gx = COX > 0 ? nwd : this.Width;
        //        int gy = COY > 0 ? nhg : this.Height;

        //        return new Size( gx , gy );
        //    }
        //}

        //public Point PointInMap( System.Drawing.Point p )
        //{
        //    return PointInMap( p.X / 32 , p.Y / 32 );
        //}
        //public Point PointInMap( Point p )
        //{
        //    return PointInMap( p.X / 32 , p.Y / 32 );
        //}
        //public Point PointInMap( int x , int y )
        //{
        //    return new Point( Graphics.Runtime.CameraOffSet_X + x / 32 , Graphics.Runtime.CameraOffSet_Y + y / 32 );
        //}

        //public bool IsInsideMapArea( Point p )
        //{
        //    return ( p.X >= 0 && p.Y >= 0 && p.X < Width && p.Y < Height );
        //}
        //public void AddLight( Light olight , Point p )
        //{
        //    this._lightsObjects[ p.Y , p.X ] = olight;
        //}


    }
}}
